package com.foolishbird.sdk.ui.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.foolishbird.sdk.core.HttpEngineApi;
import com.foolishbird.sdk.params.FloatKefuResponseParams;
import com.foolishbird.sdk.params.RedBindedResponseParams;
import com.foolishbird.sdk.params.RedGetResponseParams;
import com.foolishbird.sdk.params.ResponseParams;
import com.foolishbird.sdk.params.bean.RedBindedInfo;
import com.foolishbird.sdk.params.bean.RedPackageInfo;
import com.foolishbird.sdk.params.bean.State;
import com.foolishbird.sdk.ui.assist.OnFinishActivityListener;
import com.foolishbird.sdk.utils.MyFragmentManger;
import com.foolishbird.sdk.utils.ResUtils;
import com.foolishbird.sdk.utils.WL;
import com.squareup.picasso.Picasso;

import java.text.SimpleDateFormat;
import java.util.List;


public class RedpackageBindedFragment extends BaseHttpFragment implements View.OnClickListener {


    private ListView mListview;
    private TextView titleTxt;

    private ImageView backView;
    private  BaseAdapter mAdapter;

    public static final RedpackageBindedFragment newInstance(Bundle bundle) {
        RedpackageBindedFragment fragment = new RedpackageBindedFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        int resource = ResUtils.getLayoutIdentifier(getActivity(), "fb_redpackage_binded");
        View contentView =inflater.inflate(resource,null);
        backView = contentView.findViewById(getViewIdentifier("fb_fragment_login_back"));
        backView.setOnClickListener(this);
        titleTxt = contentView.findViewById(getViewIdentifier("fb_fragment_float_title"));
        titleTxt.setText("我的绑定");
        mListview = contentView.findViewById(getViewIdentifier("float_notice_list"));

        HttpEngineApi.getInstance().redpackageBinded(requestCallbackHandler(RedBindedResponseParams.class));
        return contentView;

    }

    private List<RedBindedInfo> myNoticeList;

    @Override
    public void onAsyncRequestResult(int flag, ResponseParams response, Object... params) {
        super.onAsyncRequestResult(flag, response, params);
        if (response != null) {
            int code = response.getState().getCode();
            if (State.SUCCESS == code) {
                if(response instanceof RedBindedResponseParams){
                   myNoticeList =((RedBindedResponseParams)response).getData().getRedPackageInfos();
                    setAdapter();
                }else if(flag == deleteTaskId){
                    HttpEngineApi.getInstance().redpackageBinded(requestCallbackHandler(RedBindedResponseParams.class));
                }
            }
        }



        WL.d("FloatNoticeFragment",response.toString());
    }

    private int deleteTaskId;

    private void setAdapter(){
        if (mAdapter==null){
            mAdapter = new BaseAdapter() {
                @Override
                public int getCount() {
                    return myNoticeList==null?0:myNoticeList.size();
                }

                @Override
                public Object getItem(int i) {
                    return myNoticeList.get(i);
                }

                @Override
                public long getItemId(int i) {
                    return i;
                }

                @Override
                public View getView(int i, View view, ViewGroup viewGroup) {
                    ViewHolder holder = null;
                    if (view == null) {
                        int resource = ResUtils.getLayoutIdentifier(getContext(), "fb_redpackage_binded_item");
                        view = LayoutInflater.from(getContext()).inflate(resource, null);
                        holder = new ViewHolder();
                        holder.typeText = (TextView) view.findViewById(ResUtils.getViewIdentifier(getContext(), "redpackage_binded_type"));
                        holder.accountText = (TextView) view.findViewById(ResUtils.getViewIdentifier(getContext(), "redpackage_binded_account"));
                        holder.nameText = (TextView) view.findViewById(ResUtils.getViewIdentifier(getContext(), "redpackage_binded_name"));
                        holder.deleteText = (TextView) view.findViewById(ResUtils.getViewIdentifier(getContext(), "redpackage_binded_delete"));

                        view.setTag(holder);
                    } else {
                        holder = (ViewHolder) view.getTag();
                    }
                    RedBindedInfo info = myNoticeList.get(i);

                    holder.typeText.setText(info.getName());
                    holder.accountText.setText(info.getAccount());
                    holder.nameText.setText(info.getRealname());
                    holder.deleteText.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            deleteTaskId = HttpEngineApi.getInstance().redpackageDelete(info.getId(),requestCallbackHandler(FloatKefuResponseParams.class));
                        }
                    });



                    return view;
                }
            };
            mListview.setAdapter(mAdapter);
        }
        mAdapter.notifyDataSetChanged();
    }

    class ViewHolder {
        TextView typeText;
        TextView accountText;
        TextView nameText;
        TextView deleteText;
    }

    SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public void onClick(View view) {
        if(view ==backView){
            MyFragmentManger.getInstance().goBack(this);
        }
//        OnFinishActivityListener listener = getOnFinishActivityListener();
//        if (listener != null) {
//            listener.doFinishActivity(this);
//        }
    }

    private OnFinishActivityListener getOnFinishActivityListener() {
        if (!isNullContext()) {
            Activity activity = getActivity();
            if (activity instanceof OnFinishActivityListener) {
                return (OnFinishActivityListener) activity;
            }
        }
        return null;
    }
}
